<?php

namespace App\ZhCN;

if (!defined('IN_PX'))
    exit;

use App\Service;
use App\Repository;
use App\Tools\Auxi;
use Phoenix\Log\Log4p as logger;

/**
 * 首页
 */
class Index {

    private function __Controller() {}

    private function __Value($__PACKAGE__, $__ROOT__, $__RM__, $setting, $cfg, $__LANGUAGE_ID__) {}

    protected function __Inject($session, Service\Archives $servArc, Repository\Archives $repoArc,
                                Repository\Category $category) {}

    public function index() {
        if ($this->servArc->chkCategoryDataView()) {
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            $_model['aryAd'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 0);
            $_model['aryAd1'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 1);
            $_model['contactHash'] = md5(time() . 'zy_contact_hash');
            $_model['aliasId1'] = 'index';
            
            if ($this->__LANGUAGE_ID__ == 1) {   // 英文的路径
                $_model['aryPartner'] = $this->repoArc->findArchives($this->__LANGUAGE_ID__, 36, 0, 10);
                $_model['aryNews'] = $this->repoArc->findArchives($this->__LANGUAGE_ID__, 40);
                $_model['product'] = $this->repoArc->findProduct(31);
                foreach ($_model['product'] as $val) {
                    $val->aryProduct = $this->repoArc->findArchives($this->__LANGUAGE_ID__, $val->category_id);
                }
            } else {
                $_model['aryPartner'] = $this->repoArc->findArchives($this->__LANGUAGE_ID__, 61, 0, 10);
                $_model['aryNews'] = $this->repoArc->findArchives($this->__LANGUAGE_ID__, 65);
                $_model['product'] = $this->repoArc->findProduct(56);
                foreach ($_model['product'] as $val) {
                    $val->aryProduct = $this->repoArc->findArchives($this->__LANGUAGE_ID__, $val->category_id);
                }
            }

            //首页底部链接
            return array (
                'model' => $_model,
                'view' => true
            );
        }
        return 404;
    }

    public function about($__Route = array('/about/index')) {
        $this->aliasId = 'historyen';
        if (!is_null(($_model = $this->servArc->getCategoryRs($this->aliasId, true)))) {
            $_model['aliasId'] = $this->aliasId;
            $_model['aliasId1'] = 'about';
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            $_model['aryAd'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 2);

            if ($_model['channelType'] == 14) {
                $_model['footerLink'] = $this->repoArc->getFooterLink($this->__LANGUAGE_ID__); //底部链接
            }

            if (!isset($this->page)) {
                $this->page = 1;
            }
            $_model['currentPage'] = $this->page;

            //根目录下的总条数
            $_model['rootCategoryTotal'] = $this->servArc->findAllRootCount($_model['rootId']);
//            if (!$_model['isPart'] && $_model['currentPage'] > ceil($_model['rootCategoryTotal'] / $_model['currentPageSize'])) {
//                return 404;
//            }
            if ($_model['categoryLevel'] == 1 && in_array($_model['channelType'], array(0, 1, 2, 4))) { //列表
                $_model['currentCategoryTotal'] = $_model['rootCategoryTotal'];
                $_model['currentListRs'] = $this->servArc->findAllRoot($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            } else {
                $_model['currentListRs'] = $this->servArc->findAll($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            }
            if ($this->__LANGUAGE_ID__ == 1) {   // 英文的路径
                $_model['product'] = $this->repoArc->findProduct(31);
            } else {
                $_model['product'] = $this->repoArc->findProduct(56);
            }


            return array(
                'model' => $_model,
                'view' => 'about/index'
            );
        }
        return 404;
    }

    public function partner($__Route = array('/partner/index')) {
        $this->aliasId = 'partneren1';
        if (!is_null(($_model = $this->servArc->getCategoryRs($this->aliasId, true)))) {
            $_model['aliasId'] = $this->aliasId;
            $_model['aliasId1'] = 'partner';
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;

            if ($_model['channelType'] == 14) {
                $_model['footerLink'] = $this->repoArc->getFooterLink($this->__LANGUAGE_ID__); //底部链接
            }

            if (!isset($this->page)) {
                $this->page = 1;
            }
            $_model['currentPage'] = $this->page;

            $_model['aryAd'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 4);

            //根目录下的总条数
            $_model['rootCategoryTotal'] = $this->servArc->findAllRootCount($_model['rootId']);
//            if (!$_model['isPart'] && $_model['currentPage'] > ceil($_model['rootCategoryTotal'] / $_model['currentPageSize'])) {
//                return 404;
//            }
            if ($_model['categoryLevel'] == 1 && in_array($_model['channelType'], array(0, 1, 2, 4))) { //列表
                $_model['currentCategoryTotal'] = $_model['rootCategoryTotal'];
                $_model['currentListRs'] = $this->servArc->findAllRoot($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            } else {
                $_model['currentListRs'] = $this->servArc->findAll($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            }
            if ($this->__LANGUAGE_ID__ == 1) {   // 英文的路径
                $_model['product'] = $this->repoArc->findProduct(31);
            } else {
                $_model['product'] = $this->repoArc->findProduct(56);
            }

            return array(
                'model' => $_model,
                'view' => 'partner/index'
            );
        }
        return 404;
    }

    public function content($__Route = '/contact/index') {
        $_model['aryAd'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 7);
        if ($this->__LANGUAGE_ID__ == 1) {   // 英文的路径
            $_model['product'] = $this->repoArc->findProduct(31);
        } else {
            $_model['product'] = $this->repoArc->findProduct(56);
        }

        return array(
            'model' => $_model,
            'view' => 'contact/index'
        );
    }

    public function crtifcation($__Route = array('/crtifcation/index/{page}')) {
        $this->aliasId = 'certifcationen1';
        if (!is_null(($_model = $this->servArc->getCategoryRs($this->aliasId, true)))) {
            $_model['aliasId'] = $this->aliasId;
            $_model['aliasId1'] = 'crtifcation';
            if ($this->__LANGUAGE_ID__ == 1) {
                $_model['aliasId2'] = 'crtifcation/index';
            } else {
                $_model['aliasId2'] = 'en/crtifcation/index';
            }
            $_model['aryAd'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 5);

            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;

            if ($_model['channelType'] == 14) {
                $_model['footerLink'] = $this->repoArc->getFooterLink($this->__LANGUAGE_ID__); //底部链接
            }

            if (!isset($this->page)) {
                $this->page = 1;
            }
            $_model['currentPage'] = $this->page;

            //根目录下的总条数
            $_model['rootCategoryTotal'] = $this->servArc->findAllRootCount($_model['rootId']);
//            if (!$_model['isPart'] && $_model['currentPage'] > ceil($_model['rootCategoryTotal'] / $_model['currentPageSize'])) {
//                return 404;
//            }
            if ($_model['categoryLevel'] == 1 && in_array($_model['channelType'], array(0, 1, 2, 4))) { //列表
                $_model['currentCategoryTotal'] = $_model['rootCategoryTotal'];
                $_model['currentListRs'] = $this->servArc->findAllRoot($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            } else {
                $_model['currentListRs'] = $this->servArc->findAll($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            }

            if ($this->__LANGUAGE_ID__ == 1) {   // 英文的路径
                $_model['product'] = $this->repoArc->findProduct(31);
            } else {
                $_model['product'] = $this->repoArc->findProduct(56);
            }

            return array(
                'model' => $_model,
                'view' => 'crtifcation/index'
            );
        }
        return 404;
    }

    public function news($__Route = array('/news/index/{page}')) {
        $this->aliasId = 'newsen1';
        if (!is_null(($_model = $this->servArc->getCategoryRs($this->aliasId, true)))) {
            $_model['aliasId'] = $this->aliasId;
            $_model['aliasId1'] = 'news';
            if ($this->__LANGUAGE_ID__ == 1) {
                $_model['aliasId2'] = 'news/index';
            } else {
                $_model['aliasId2'] = 'en/news/index';
            }
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;

            $_model['aryAd'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 6);

            if ($_model['channelType'] == 14) {
                $_model['footerLink'] = $this->repoArc->getFooterLink($this->__LANGUAGE_ID__); //底部链接
            }

            if (!isset($this->page)) {
                $this->page = 1;
            }
            $_model['currentPage'] = $this->page;

            //根目录下的总条数
            $_model['rootCategoryTotal'] = $this->servArc->findAllRootCount($_model['rootId']);
//            if (!$_model['isPart'] && $_model['currentPage'] > ceil($_model['rootCategoryTotal'] / $_model['currentPageSize'])) {
//                return 404;
//            }
            if ($_model['categoryLevel'] == 1 && in_array($_model['channelType'], array(0, 1, 2, 4))) { //列表
                $_model['currentCategoryTotal'] = $_model['rootCategoryTotal'];
                $_model['currentListRs'] = $this->servArc->findAllRoot($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            } else {
                $_model['currentListRs'] = $this->servArc->findAll($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            }

            if ($this->__LANGUAGE_ID__ == 1) {   // 英文的路径
                $_model['product'] = $this->repoArc->findProduct(31);
            } else {
                $_model['product'] = $this->repoArc->findProduct(56);
            }

            return array(
                'model' => $_model,
                'view' => 'news/index'
            );
        }
        return 404;
    }

    public function newsDetail($__Route = array('/news/detail/{id}')) {
        $_model['aliasId1'] = 'news';
        if ($this->__LANGUAGE_ID__ == 1) {
            $_GET['aliasId2'] = '/news/detail/';
        } else {
            $_GET['aliasId2'] = '/en/news/detail/';
        }
        if (!is_null(($_model = $this->servArc->getSubstance($this->id)))) {
            $_parentsId = array_filter(explode('.', $_model['idTree']));
            $_model['parentId'] = intval($_parentsId[2]);

            $_model['aryAd'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 6);

            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;

//            var_dump($_model['news']);die;
            if ($this->__LANGUAGE_ID__ == 1) {   // 英文的路径
                $_model['news'] = $this->repoArc->findArchives1($this->__LANGUAGE_ID__, 40);
                $_model['product'] = $this->repoArc->findProduct(31);
            } else {
                $_model['news'] = $this->repoArc->findArchives1($this->__LANGUAGE_ID__, 65);
                $_model['product'] = $this->repoArc->findProduct(56);
            }

            return array(
                'model' => $_model,
                'view' => 'news/detail'
            );
        }
        return 404;
    }

    public function products($__Route = array('/products/index/{page}'))
    {
		$this->aliasId = 'productsen';
		if (!is_null(($_model = $this->servArc->getCategoryRs($this->aliasId, true)))) {
        $this->aliasId = 'products';
        $_model['aliasId'] = $this->aliasId;
        $_model['aliasId1'] = 'products';
        $_model['separator'] = '/';
        $id = explode('-', $this->page);
        foreach ($id as $k=>$val) {
            $page[$k] = intval($val);
        }
        if ($this->__LANGUAGE_ID__ == 1) {
            if (!isset($page[0]) || $page[0] == 0) {
                $_model['categoryId'] = $this->repoArc->findCategoryId($_model['rootId']);
            } else {
                $_model['categoryId'] = $page[0];
            }
            $_model['aliasId2'] = 'products/index';
        } else {
            if (!isset($page[0]) || $page[0] == 0) {
                 $_model['categoryId'] = $this->repoArc->findCategoryId($_model['rootId']);
            } else {
                $_model['categoryId'] = $page[0];
            }
            $_model['aliasId2'] = 'en/products/index';
        }
//            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;

//            if ($_model['channelType'] == 14) {
//                $_model['footerLink'] = $this->repoArc->getFooterLink($this->__LANGUAGE_ID__); //底部链接
//            }

        if (!isset($page[1]) || !is_numeric($page[1]) || ($page[1] == 0)) {
            $_model['currentPage'] = 1;
        } else {
            $_model['currentPage'] = $page[1];
        }

        $_model['aryAd'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 3);

        $_model['currentPageSize'] = 12;

        $_model['currentCategoryTotal'] = $this->repoArc->findAllCategoryCount($_model['categoryId']);

        $_model['currentListRs'] = $this->servArc->findAll($_model['categoryId'],
            $_model['currentPageSize'] * ($_model['currentPage'] - 1),
            $_model['currentPageSize']);

        if ($this->__LANGUAGE_ID__ == 1) {   // 英文的路径
            $_model['product'] = $this->repoArc->findProduct(31);
        } else {
            $_model['product'] = $this->repoArc->findProduct(56);
        }

        return array(
            'model' => $_model,
            'view' => 'products/index'
        );
		} 
		return 404;
    }

    public function productDetail($__Route = array('/products/detail/{id}')) {
        $_model['aliasId1'] = 'products';
        if ($this->__LANGUAGE_ID__ == 1) {
            $_GET['aliasId2'] = '/products/detail/';
        } else {
            $_GET['aliasId2'] = '/en/products/detail/';
        }
        if (!is_null(($_model = $this->servArc->getSubstance($this->id)))) {
            $_parentsId = array_filter(explode('.', $_model['idTree']));
            $_model['parentId'] = intval($_parentsId[2]);

            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;

            if ($this->__LANGUAGE_ID__ == 1) {   // 英文的路径
                $_model['product'] = $this->repoArc->findProduct(31);
            } else {
                $_model['product'] = $this->repoArc->findProduct(56);
            }
            $_model['src'] = $this->repoArc->findArchivesSrc($this->id);

            return array(
                'model' => $_model,
                'view' => 'products/detail'
            );
        }
        return 404;
    }
}
